package com.itheima.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.Test;

// JDBC快速入门
public class JDBCDemo_ResultSet {
    /**
     * @throws Exception
     */

    @Test
    public void testResultSet() throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取连接               // localhost
        String url = "jdbc:mysql://127.0.0.1:3308/db1?useSSL=false";// &characterEncoding=UTF-8
                    //jdbc:mysql:///db1?useSSL=false
        String username = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url,username,password);
        //3.定义sql
        String sql = "select * from account";

        //4.获取statement对象
        Statement stmt = conn.createStatement();

        //5.执行sql
        ResultSet rs =  stmt.executeQuery(sql);

        //6.处理结束，遍历rs中的所有数据
            //光标向下移动一行，并且判断当前行是否有数据
        while (rs.next()){
            //获取数据  getXxx()
            int id = rs.getInt(1);
            String name = rs.getString(2);
            double money = rs.getDouble(3);
            System.out.println("id:"+id+"  name:"+name+"  money:"+money);

        }

        //7.释放资源
        rs.close();
        stmt.close();
        conn.close();

    }

}
